import { RequestPayload, } from "@core/types"
import { parseJSON } from "@tools/core"
import { useSecret, useToken } from "./utils"
import * as jwt from 'jsonwebtoken'
import { toError } from "./to"
import { WebSocket } from "ws"
import { listenRequestClientIdQueue } from "./listenRequestClientIdQueue"
import { listenRequestQueue } from "./listenRequestQueue"
export function listenRequestQueueWs(uuid: string, appName: string, ws: WebSocket) {
    return listenRequestClientIdQueue(uuid, appName, async (msg: RequestPayload) => {
        try {
            const data = parseJSON(msg.data.toString('utf8'))
            const headers = parseJSON(msg.headers.toString('utf8'))
            const token = useToken(data, headers)
            const path = msg.path;
            const method = msg.method;
            const name = msg.name;
            const appid = msg.appid;
            const reqid = msg.reqid;
            const ip = msg.ip;
            const version = msg.version;
            let user: any = null;
            try {
                const secret = useSecret()
                user = jwt.verify(token, secret)
            } catch (e) { }
            // token 解析
            console.log(`任务已下发，正在处理中...`)
            ws.send(JSON.stringify({
                type: 'REQUEST',
                payload: {
                    data,
                    headers,
                    user,
                    path,
                    method,
                    name,
                    appid,
                    reqid,
                    ip,
                    version
                }
            }));
        } catch (e) {
            console.error(e)
            const err: Error = e as any;
            return toError(uuid, err, msg)
        }
    })
}

export function listenRequestMainQueueWs(uuid: string, appName: string, ws: WebSocket) {
    return listenRequestQueue(uuid, appName, async (msg: RequestPayload) => {
        try {
            const data = parseJSON(msg.data.toString('utf8'))
            const headers = parseJSON(msg.headers.toString('utf8'))
            const token = useToken(data, headers)
            const path = msg.path;
            const method = msg.method;
            const name = msg.name;
            const appid = msg.appid;
            const reqid = msg.reqid;
            const ip = msg.ip;
            const version = msg.version;
            let user: any = null;
            try {
                const secret = useSecret()
                user = jwt.verify(token, secret)
            } catch (e) { }
            // token 解析
            console.log(`任务已下发，正在处理中...`)
            ws.send(JSON.stringify({
                type: 'REQUEST',
                payload: {
                    data,
                    headers,
                    user,
                    path,
                    method,
                    name,
                    appid,
                    reqid,
                    ip,
                    version
                }
            }));
        } catch (e) {
            console.error(e)
            const err: Error = e as any;
            return toError(uuid, err, msg)
        }
    })
}